﻿using System;
using System.IO;
using System.Net;
using System.Text;

namespace GoogleTranslate
{
    /// <summary>
    /// Summary description for GoogleTranslate
    /// </summary>
    public static class Translator
    {
        public static string Translate(string translateText, string toLanguage)
        {
            string translateUrl = "http://translate.google.com.vn/?ie=UTF-8&text={0}&sl=auto&tl={1}";
            translateUrl = String.Format(translateUrl, translateText, toLanguage);

            if (translateUrl.Length > 1490)
            {
                return Translator.TranslateLargeText(translateText, toLanguage);
            }

            WebClient webClient = new WebClient();

            string result = webClient.DownloadString(translateUrl);

            int startSubstring = result.IndexOf("result_box");

            result = result.Substring(startSubstring);

            startSubstring = result.IndexOf(">") + 1;

            result = result.Substring(startSubstring, result.IndexOf("</div>") - startSubstring);

            //startSubstring = result.IndexOf(">") + 1;

            //result = result.Substring(startSubstring);

            result = HtmlRemoval.StripTagsCharArray(result);

            return (translateUrl.Length.ToString() + result);
        }

        public static string Translate(string translateText, string fromLanguage, string toLanguage)
        {
            string translateUrl = "http://translate.google.com.vn/?ie=UTF-8&text={0}&sl={1}&tl={2}";
            translateUrl = String.Format(translateUrl, translateText, fromLanguage, toLanguage);

            if (translateUrl.Length > 1490)
            {
                return Translator.TranslateLargeText(translateText, fromLanguage, toLanguage);
            }

            WebClient webClient = new WebClient();

            string result = webClient.DownloadString(translateUrl);

            int startSubstring = result.IndexOf("result_box");

            result = result.Substring(startSubstring);

            startSubstring = result.IndexOf(">") + 1;

            result = result.Substring(startSubstring, result.IndexOf("</span>") - startSubstring);

            startSubstring = result.IndexOf(">") + 1;

            result = result.Substring(startSubstring);
            
            result = HtmlRemoval.StripTagsCharArray(result);

            return result;
        }

        public static string Translate(string translateText, string fromLanguage, string toLanguage, string encodeText)
        {
            string translateUrl = "http://translate.google.com.vn/?text={0}&sl={1}&tl={2}&ie={3}";
            translateUrl = String.Format(translateUrl, translateText, fromLanguage, toLanguage, encodeText);

            if (translateUrl.Length > 1490)
            {
                return Translator.TranslateLargeText(translateText, fromLanguage, toLanguage, encodeText);
            }

            WebClient webClient = new WebClient();

            string result = webClient.DownloadString(translateUrl);

            int startSubstring = result.IndexOf("result_box");

            result = result.Substring(startSubstring);

            startSubstring = result.IndexOf(">") + 1;

            result = result.Substring(startSubstring, result.IndexOf("</span>") - startSubstring);

            startSubstring = result.IndexOf(">") + 1;

            result = result.Substring(startSubstring);

            result = HtmlRemoval.StripTagsCharArray(result);

            return result;
        }

        public static string TranslateLargeText(string translateText, string toLanguage)
        {
            var req = WebRequest.Create("http://translate.google.com.vn/");

            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";

            string paras = "ie=UTF-8&text=" + translateText + "&sl=auto&tl=" + toLanguage;

            byte[] bytes = Encoding.UTF8.GetBytes(paras);

            req.ContentLength = bytes.Length;
            Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
            os.Close();

            WebResponse resp = req.GetResponse();
            if (resp == null)
            {
                return "";
            }

            var sr = new StreamReader(resp.GetResponseStream());

            string result = sr.ReadToEnd().Trim();

            int startSubstring = result.IndexOf("result_box");

            result = result.Substring(startSubstring);

            startSubstring = result.IndexOf(">") + 1;
                        
            result = result.Substring(startSubstring, result.IndexOf("</div>") - startSubstring);
            result = HtmlRemoval.StripTagsCharArray(result);

            return result;
        }

        public static string TranslateLargeText(string translateText, string fromLanguage, string toLanguage)
        {
            var req = WebRequest.Create("http://translate.google.com.vn/");

            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";

            string paras = "ie=UTF-8&text=" + translateText
                + "&sl=" + fromLanguage + "&tl=" + toLanguage;

            byte[] bytes = Encoding.UTF8.GetBytes(paras);

            req.ContentLength = bytes.Length;
            Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
            os.Close();

            WebResponse resp = req.GetResponse();
            if (resp == null)
            {
                return "";
            }

            var sr = new StreamReader(resp.GetResponseStream());

            string result = sr.ReadToEnd().Trim();

            int startSubstring = result.IndexOf("result_box");

            result = result.Substring(startSubstring);

            startSubstring = result.IndexOf(">") + 1;

            result = result.Substring(startSubstring, result.IndexOf("</div>") - startSubstring);
            result = HtmlRemoval.StripTagsCharArray(result);

            return result;
        }

        public static string TranslateLargeText(string translateText, string fromLanguage, string toLanguage, string encodeText)
        {
            var req = WebRequest.Create("http://translate.google.com.vn/");

            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";

            string paras = "ie=UTF-8&text=" + translateText 
                + "&sl=" + fromLanguage + "&tl=" + toLanguage 
                + "&ie=" + encodeText;

            byte[] bytes = Encoding.UTF8.GetBytes(paras);

            req.ContentLength = bytes.Length;
            Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
            os.Close();

            WebResponse resp = req.GetResponse();
            if (resp == null)
            {
                return "";
            }

            var sr = new StreamReader(resp.GetResponseStream());

            string result = sr.ReadToEnd().Trim();

            int startSubstring = result.IndexOf("result_box");

            result = result.Substring(startSubstring);

            startSubstring = result.IndexOf(">") + 1;

            result = result.Substring(startSubstring, result.IndexOf("</div>") - startSubstring);
            result = HtmlRemoval.StripTagsCharArray(result);

            return result;
        }
    }
}